<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .card img {
            width: 60px;
            height: 60px;
        }
    </style>
    <script src="../lib/vue.js"></script>
</head>

<body>
    <div id='app'>
        <div v-pin="card1.pinded" class="card">
            <a href="#" @click.prevent='card1.pinded = !card1.pinded'>钉住/取消</a>
            <img src="../lib/IMG_7128.jpeg" alt="">
        </div>
        <div v-pin="card2.pinded" class="card">
            <a href="#" @click.prevent='card2.pinded = !card2.pinded'>钉住/取消</a>
            <img src="../lib/IMG_7128.jpeg" alt="">
        </div>
    </div>
    </div>
    <script>
        let toppx = 10
        Vue.directive('pin', (el, binding) => {
            if (binding.value) {
                el.style.position = "fixed"
                el.style.left = '300px'
                let top = toppx
                el.style.top = top + 'px'
                toppx += 200
            } else {
                el.style.position = 'static'
                toppx === 10 ? toppx : toppx -= 200
            }
        })
        var app = new Vue({
            el: '#app',
            data: {
                card1: {
                    pinded: false
                },
                card2: {
                    pinded: false
                }
            },
        })
    </script>
</body>

</html>